package cn.crism.blog.service.impl;

import cn.crism.blog.entity.Article;
import cn.crism.blog.param.QueryParam;
import cn.crism.blog.repository.ArticleRepository;
import cn.crism.blog.service.ArticleService;
import cn.crism.blog.service.CommentService;
import cn.crism.blog.util.DateUtil;
import cn.crism.blog.vo.ArchiveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;

/**
 * @Author: Crism
 * @Date: 上午9:51 18/2/6
 * @Description:
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private CommentService commentService;

    @Override
    public String publishArticle(Article article) {
        if (article.getArticleTitle()==null || article.getArticleTitle().length()<1)
            return "请输入标题！";
        if (article.getArticleAuthor()==null || article.getArticleAuthor().length()<1)
            return "请输入作者名称！";
        if (article.getArticleContent()==null || article.getArticleContent().length()<1)
            return "请输入！";
        try {
            if (article.getArticleId() == null){
                article.setUpdateTime(new Date());
            }

            articleRepository.save(article);
            return "成功！";
        }catch (Exception e){
            return "失败！";
        }
    }

    @Override
    public Page<Article> getAllArticles(QueryParam queryParam) {
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        PageRequest pageable = new PageRequest(queryParam.getPage(), queryParam.getSize(),sort);
        return articleRepository.findAll(pageable);
    }

    @Override
    public Page<Article> getArticles(QueryParam queryParam) {
        PageRequest pageable = new PageRequest(queryParam.getPage(), queryParam.getSize());
        if (queryParam.getTags() != null) {
            return articleRepository.findAllByArticleStatusAndArticleTagsLikeOrderByUpdateTimeDesc(false,"%" + queryParam.getTags() + "%",pageable);
        } else if (queryParam.getMixParam() != null) {
            return articleRepository.findAllByArticleStatusAndArticleTitleLike(false,"%" + queryParam.getMixParam() + "%",pageable);
        } else {
            return articleRepository.findAllByArticleStatusOrderByUpdateTimeDesc(false,pageable);
        }
    }

    @Override
    @Transactional
    public Boolean removeArticle(Integer articleId) {
        if (articleRepository.findById(articleId) != null) {
            try {
                commentService.removeComments(articleId);
                articleRepository.delete(articleId);
                return true;
            }catch (Exception e){
                return false;
            }
        }
        return false;
    }

    @Override
    public String getAticleTitle(Integer articleId) {
        Article article = articleRepository.findOne(articleId);
        if (article!=null)
            return article.getArticleTitle();
        return null;
    }

    /**
     * 文章归档信息获取
     * @return Articles
     */
    @Transactional
    public Map<String,Object> findArticleGroupByTime(QueryParam queryParam){
        List<Object[]> lists= articleRepository.findArticleGroupByTime(queryParam.getStartRow(), queryParam.getSize());
        Integer totalCount = articleRepository.countArticleByTime();
        Map<String,Object> map = new HashMap<>();
        List<ArchiveVO> archiveVOS =new ArrayList<>();
        for (Object[] o:lists) {
            List<Article> articles = articleRepository.findArticleByMonth(DateUtil.strToDate( o[0]+"-"+o[1]));
            archiveVOS.add(new ArchiveVO(o,articles));
        }
        map.put("totalcount",totalCount);
        map.put("archives", archiveVOS);
        return map;
    }

    /**
     * 文章归档信息获取
     * @param articleId
     * @return Article
     */
    @Override
    public Article getArticleByArticleId(Integer articleId) {
        Article article = articleRepository.findOne(articleId);
        if (article!=null){
            return article;
        }
        return null;
    }

//    @Transactional
//    @Override
//    public List<Article> findArticleByMonth(Date month) {
//        return articleRepository.findArticleByMonth(month);
//    }

}
